<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
 var url;
 function searchRole(){
	 $("#dg").datagrid('load',{
		"roleName":$("#s_roleName").val() 
	 });
 }
 
 function openRoleAddDialog(){
	 $("#dlg").dialog("open").dialog("setTitle","添加角色信息");
	 url="${pageContext.request.contextPath}/role/save";
 }
 
 function openRoleModifyDialog(){
	 var selectedRows=$("#dg").datagrid("getSelections");
	 if(selectedRows.length!=1){
		 $.messager.alert("系统提示","请选择一条要编辑的数据！");
		 return;
	 }
	 var row=selectedRows[0];
	 $("#dlg").dialog("open").dialog("setTitle","编辑角色信息");
	 $("#fm").form("load",row);
	 url="${pageContext.request.contextPath}/role/save?roleId="+row.roleId;
 }
 
 function saveRole(){
	 $("#fm").form("submit",{
		url:url,
		onSubmit:function(){
			return $(this).form("validate");
		},
		success:function(result){
			var result=eval('('+result+')');
			if(result.success){
				$.messager.alert("系统提示","保存成功！");
				resetValue();
				$("#dlg").dialog("close");
				$("#dg").datagrid("reload");
			}else{
				$.messager.alert("系统提示","保存失败！");
				return;
			}
		}
	 });
 }
 
 function resetValue(){
	 $("#roleName").val("");
	 $("#description").val("");
 }
 
 function closeRoleDialog(){
	 $("#dlg").dialog("close");
	 resetValue();
 }
 
 function deleteRole(){
	 var selectedRows=$("#dg").datagrid("getSelections");
	 if(selectedRows.length==0){
		 $.messager.alert("系统提示","请选择要删除的数据！");
		 return;
	 }
	 var strIds=[];
	 for(var i=0;i<selectedRows.length;i++){
		 strIds.push(selectedRows[i].roleId);
	 }
	 var ids=strIds.join(",");
	 $.messager.confirm("系统提示","您确定要删除这<font color=red>"+selectedRows.length+"</font>条数据吗？",function(r){
		if(r){
			$.post("${pageContext.request.contextPath}/role/delete",{ids:ids},function(result){
				if(result.success){
					 $.messager.alert("系统提示","数据已成功删除！");
					 $("#dg").datagrid("reload");
				}else{
					$.messager.alert("系统提示","数据删除失败，请联系系统管理员！");
				}
			},"json");
		} 
	 });
 }
 
 function openRoleDialog(){
	 var selectedRows=$("#dg").datagrid('getSelections');
		if(selectedRows.length!=1){
			$.messager.alert('系统提示','请选择一条要授权的角色！');
			return;
	   }
	var row=selectedRows[0];
	roleId=row.roleId;
	$("#dlg2").dialog("open").dialog("setTitle","角色授权");
	url="${pageContext.request.contextPath}/resource/authMenu?parentId=-1&roleId="+roleId;
	$("#tree").tree({
		lines:true,
		url:url,
		checkbox:true,
		cascadeCheck:false,
		onLoadSuccess:function(){
			$("#tree").tree('expandAll');
		},
		onCheck:function(node,checked){
			if(checked){
				checkNode($('#tree').tree('getParent',node.target));
			}
		}
	});
 }
 function checkNode(node){
		if(!node){
			return;
		}else{
			checkNode($('#tree').tree('getParent',node.target));
			$('#tree').tree('check',node.target);
		}
 }
 
 function saveAuth(){
	 var nodes=$('#tree').tree('getChecked');
		var resourceArrIds=[];
		for(var i=0;i<nodes.length;i++){
			resourceArrIds.push(nodes[i].id);
		}
		var resourceIds=resourceArrIds.join(",");
		$.post("${pageContext.request.contextPath}/role/authRole",{resourceIds:resourceIds,roleId:roleId},function(result){
			if(result.success){
				$.messager.alert('系统提示','授权成功！');
				closeAuthDialog();
			}else{
				$.messager.alert('系统提示',result.errorMsg);
			}
		},"json");
 }
 function closeAuthDialog(){
		$("#dlg2").dialog("close");
	}
</script>
<title>角色管理</title>
</head>
<body style="margin: 1px">
 <table id="dg" title="角色管理" class="easyui-datagrid"
   fitColumns="true" pagination="true" rownumbers="true"
   url="${pageContext.request.contextPath}/role/list" fit="true" toolbar="#tb">
   <thead>
   	<tr>
   		<th field="cb" checkbox="true" align="center"></th>
   		<th field="roleName" width="60" align="center">角色昵称</th>
   		<th field="description" width="200" align="center">角色描述</th>
   	</tr>
   </thead>
 </table>
 <div id="tb">
 	<div>
 		<a href="javascript:openRoleAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
 		<a href="javascript:openRoleModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
 		<a href="javascript:deleteRole()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
 		<a href="javascript:openRoleDialog()" class="easyui-linkbutton" iconCls="icon-roleManage" plain="true">角色授权</a>
 	</div>
 	<div>
 		&nbsp;角色昵称：&nbsp;<input type="text" id="s_roleName" size="20" onkeydown="if(event.keyCode==13) searchRole()"/>
 		<a href="javascript:searchRole()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
 	</div>
 </div>
 
 <div id="dlg" class="easyui-dialog" style="width:520px;height:200px;padding: 10px 20px"
   closed="true" buttons="#dlg-buttons">
   
   <form id="fm" method="post">
   	<table cellspacing="8px">
   		<tr>
   			<td>角色昵称：</td>
   			<td><input type="text" id="roleName" name="roleName" class="easyui-validatebox" required="true"/>&nbsp;<font color="red">*</font></td>
   		</tr>
   		 <tr>
   		    <td>角色描述：</td>
   			<td><textarea rows="3" cols="30" id="description" name="description"></textarea></td>
   		</tr>
   	</table>
   </form>
 </div>
 
 <div id="dlg-buttons">
 	<a href="javascript:saveRole()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
 	<a href="javascript:closeRoleDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
 </div>
 
 <div id="dlg2" class="easyui-dialog" style="width: 300px;height: 450px;padding: 10px 20px"
  closed="true" buttons="#dlg2-buttons">
	<ul id="tree" class="easyui-tree"></ul>
  </div>
 <div id="dlg2-buttons">
	<a href="javascript:saveAuth()" class="easyui-linkbutton" iconCls="icon-ok" >授权</a>
	<a href="javascript:closeAuthDialog()" class="easyui-linkbutton" iconCls="icon-cancel" >关闭</a>
</div>
</body>
</html>